﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace org.ids_adi.iring.adapter
{
  using org.ids_adi.iring.adapter.dtoLayer;
  using org.ids_adi.iring.adapter.dtoLayer.Model;
  using org.ids_adi.iring.adapter.eSUB;
  using org.ids_adi.iring.adapter.dtoLayer.Model.eSUB;
  using org.ids_adi.iring.adapter.dtoLayer.eSUB;
  using Microsoft.Practices.Unity;
  using Microsoft.Practices.Unity.Configuration;
  using System.Configuration;

  public partial class DataService : IDataService
  {
    private DataServiceProvider _dataServiceProvider = null;


    /// <summary>
    /// Adapter Service Constructor
    /// </summary>
    public DataService()
    {
      _dataServiceProvider = new DataServiceProvider();
    }

    public org.ids_adi.iring.adapter.dtoLayer.Model.eSUB.Valves GetValvesREST(string identifier)
    {
      return _dataServiceProvider.GetValvesREST(identifier);
    }

    public Dictionary<string, string> GetValvesListREST()
    {
      return _dataServiceProvider.GetValvesListREST();
    }

    public org.ids_adi.iring.adapter.eSUB.ValvesResponse GetValves(Identifier identifier)
    {
      return _dataServiceProvider.GetValves(identifier);
    }

    public org.ids_adi.iring.adapter.eSUB.ValvesListResponse GetValvesList()
    {
      return _dataServiceProvider.GetValvesList();
    }  

  }

  public partial class DataServiceProvider : IDataService
  {
    IUnityContainer _container = null;
    private IDTOLayer _dtoFactory = null;
    private ConfigSettings _configSettings = null;

    public DataServiceProvider()
    {
      _configSettings = new ConfigSettings();
      _container = new UnityContainer();
      UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
      section.Containers.Default.Configure(_container);
      _dtoFactory = _container.Resolve<IDTOLayer>(_configSettings.DtoLayerName);

    }

    public org.ids_adi.iring.adapter.dtoLayer.Model.eSUB.Valves GetValvesREST(string identifier)
    {
      try
      {
        return (org.ids_adi.iring.adapter.dtoLayer.Model.eSUB.Valves)_dtoFactory.GetDTO("Valves", identifier);
      }
      catch (Exception exception)
      {
        throw new Exception("Error while getting Valves DTO with identifier " + identifier + ". " + exception.ToString(), exception);
      }
    }

    public Dictionary<string, string> GetValvesListREST()
    {
      try
      {
        return _dtoFactory.GetDTOList("Valves");
      }
      catch (Exception exception)
      {
        throw new Exception("Error while getting ValvesDTO list. " + exception.ToString(), exception);
      }
    }

    public org.ids_adi.iring.adapter.eSUB.ValvesResponse GetValves(Identifier identifier)
    {
      try
      {
        org.ids_adi.iring.adapter.eSUB.ValvesResponse response = new org.ids_adi.iring.adapter.eSUB.ValvesResponse();
        response.dto = (org.ids_adi.iring.adapter.dtoLayer.Model.eSUB.Valves)_dtoFactory.GetDTO("Valves", identifier.value);

        return response;
      }
      catch (Exception exception)
      {
        throw new Exception("Error while getting Valves DTO with identifier " + identifier.value + ". " + exception.ToString(), exception);
      }
    }

    public org.ids_adi.iring.adapter.eSUB.ValvesListResponse GetValvesList()
    {
      try
      {
        List<DataTransferObject> commonDTOList = (List<DataTransferObject>)(_dtoFactory.GetList("Valves"));

        List<Valves> ValvesList = new List<Valves>();
        foreach (DataTransferObject dto in commonDTOList)
        {
          ValvesList.Add((org.ids_adi.iring.adapter.dtoLayer.Model.eSUB.Valves)dto);
        }

        org.ids_adi.iring.adapter.eSUB.ValvesListResponse response = new org.ids_adi.iring.adapter.eSUB.ValvesListResponse();
        response.dtoList = ValvesList;

        return response;
      }
      catch (Exception exception)
      {
        throw new Exception("Error while getting Valves DTO list. " + exception.ToString(), exception);
      }
    }

   
  }
}